package com.ibm.mq.point.binder;

import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.stream.binder.Binding;
import org.springframework.cloud.stream.binder.ConsumerProperties;
import org.springframework.cloud.stream.binder.ProducerProperties;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.messaging.MessageChannel;
import org.springframework.messaging.support.GenericMessage;

import javax.annotation.Resource;
import javax.jms.*;

@Slf4j
public class JmsSingleBinderIbmMQ extends JmsBinderAbstract {

    @Resource(name = "pointJmsTemplate")
    private JmsTemplate jmsTemplate;

    @Override
    public Binding<MessageChannel> bindConsumer(String name, String group, MessageChannel inboundBindTarget, ConsumerProperties consumerProperties) {
        listenerConsumer(jmsTemplate,inboundBindTarget, session-> {
            try {
                return session.createQueue(name);
            } catch (JMSException e) {
                log.error("监听IBM MQ点对点的消息Topic: {}信息异常",name);
                log.error("监听IBM MQ点对点的消息Topic信息异常",e);
            }
            return null;
        });
        return () -> {
        };
    }


    @Override
    public Binding<MessageChannel> bindProducer(String name, MessageChannel outboundBindTarget, ProducerProperties producerProperties) {
        sendProducer(jmsTemplate,name,outboundBindTarget);
        return () -> System.out.println("Unbind");
    }
}
